package com.example.controller;


import com.example.common.Result;
import com.example.dao.*;
import com.example.entity.*;
import com.example.util.Savesession;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

//前台首页
@CrossOrigin//解决跨域问题注解
@RestController
public class IndexController {
    @Resource
    NewsDao newsDao;
    @Resource
    ImgadvDao imgadvDao;
    @Resource
    GoodsDao goodsDao;
    @Resource
    InventoryDao inventoryDao;
    @Resource
    CategoryDao categoryDao;
    @Resource
    Savesession savesession;
    @Resource
    EvaluateDao evaluateDao;

    //查询前台首页信息
    @ResponseBody
    @RequestMapping("index")
    public Result index(HttpServletRequest request){
        HashMap<String,Object> res = new HashMap<String,Object>();
        String key1 = request.getParameter("key1");
        String istj = request.getParameter("istj");
        HashMap map = new HashMap();
        //查询公告信息
        List<News> list = newsDao.selectAll(null);
        //查询轮播图信息
        List<Imgadv> imglist = imgadvDao.selectAll(null);
        //查询菜品信息
        map.put("key1",key1);
        map.put("istj",istj);
        List<Goods> goodsList = goodsDao.selectAll(map);
        for (Goods goods : goodsList) {
            Category category = categoryDao.findById(goods.getCategoryid());
            goods.setCatename(category.getName());
            int kcnum = savesession.getInventory(goods.getId(),request);
            goods.setKcnum(kcnum);
        }
        //查询分类信息
        List<Category> cateList = categoryDao.selectAll(null);
        //查询菜品平均评分
        List<Evaluate> evList = evaluateDao.selectPJF(null);
        for (Evaluate evaluate : evList) {
            Goods goods = goodsDao.findById(evaluate.getGoodsid());
            evaluate.setGoods(goods);
        }

        res.put("evList", evList);
        res.put("nlist", list);
        res.put("imglist", imglist);
        res.put("goodsList", goodsList);
        res.put("cateList", cateList);
        return Result.success(res);
    }


}
